/*
 * The MIT License
 *
 * Copyright 2012 http://generic-toolset.googlecode.com.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.rlpw.swing;

import java.util.Collection;

/**
 *
 * @param <T> the type of the elements
 */
public abstract class DAOModel<T> extends DefaultModel<T> implements DataAccessObject<T> {

    private boolean init = true;

    public DAOModel() {
        super.addAll(init());
        init = false;
    }

    public void reload() {
        clear();
        init = true;
        super.addAll(init());
        init = false;
    }

    @Override
    public boolean add(T e) {
        if (create0(e)) {
            return super.add(e);
        } else {
            return false;
        }
    }

    @Override
    public void add(int index, T element) {
        if (create0(element)) {
            super.add(index, element);
        }
    }

    @Override
    public boolean addAll(int index, Collection<? extends T> c) {
        if (init) {
            return super.addAll(index, c);
        }
        if (c == null || index > size() || index < 0) {
            return false;
        } else {
            int i = 0;
            for (T item : c) {
                if (create0(item)) {
                    super.add(index + i, item);
                    i++;
                }
            }
            if (i > 0) {
                updateViews();
                fireIntervalAdded(this, index, index + i - 1);
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public T remove(int index) {
        if (delete0(get(index))) {
            return super.remove(index);
        } else {
            return null;
        }
    }

    @Override
    public boolean remove(Object o) {
        if (delete0((T) o)) {
            return super.remove(o);
        } else {
            return false;
        }
    }

    @Override
    public T set(int index, T element) {
        if ((!contains(element) || get(index).equals(element))
                && (delete0(get(index)) && create0(element))) {
            return super.set(index, element);
        } else {
            return null;
        }
    }

    @Override
    protected synchronized void fireChanged(T item, int index) {
        update(item);
        super.fireChanged(item, index);
    }

    private boolean create0(T element) {
        try {
            create(element);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private boolean delete0(T element) {
        try {
            delete(element);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
